package com.xm2.demo.system.customer.controller;

import com.xm2.demo.model.Complaint;
import com.xm2.demo.model.User;
import com.xm2.demo.system.customer.ov.ComplaintOv;
import com.xm2.demo.system.customer.ov.UserOv;
import com.xm2.demo.system.customer.service.ComplaintService;
import com.xm2.demo.system.customer.service.CustomerService;
import com.xm2.demo.system.customer.service.EvaluateService;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @Create
 * @create 2018-12-12 16:29
 * @Description： 顾客
 */
@Controller
@RequestMapping(value = "/system/")
public class CustomerController {
    @Autowired
    @Qualifier("customerService")
    private CustomerService customerService;
    @Autowired
    @Qualifier("evaluateService")
    private EvaluateService evaluateService;
    @Autowired
    @Qualifier("complaintService")
    private ComplaintService complaintService;
    // 分页查询
    @GetMapping(value = "user")
    public String toUser(Model model){
        Pagination pagination = new Pagination();
        pagination =  customerService.findByPositionId(pagination);
        model.addAttribute("pagination",pagination);
        return "user";
    }
    // 分页
    @PostMapping(value = "user")
    @ResponseBody
    public Pagination doUser(@RequestBody Pagination pagination,Model model){
        pagination =  customerService.findByPositionId(pagination);
        return pagination;
    }

    @PostMapping(value = "updateUser")
    @ResponseBody
    public String updateUser(@RequestBody User user){
        try{
            customerService.updateUser(user);
            return "SUCCESS";
        }catch (Exception e){
            return "fail";
        }
    }

    @PostMapping(value = "addUser")
    @ResponseBody
    public String addUser(@RequestBody User user){
        try{
            customerService.addUser(user);
            return "SUCCESS";
        }catch (Exception e){
            return "fail";
        }
    }
    // 根据username查询
    @PostMapping(value = "selectUsername")
    @ResponseBody
    public List<User> selectUsername(@RequestBody User user){
        return customerService.findByUsernameAndPositionId(user.getUsername(),0);
    }
    // 删除用户
    @PostMapping(value = "deleteUsername")
    @ResponseBody
    public String deleteUsername(@RequestBody UserOv userOv){
        try{
            customerService.deleteUsername(userOv.getIds());
            return "SUCCESS";
        }catch (Exception e){
            return "fail";
        }
    }



    @GetMapping(value = "evaluate")
    public String toEvaluate(Model model){
        Pagination pagination = new Pagination();
        pagination = evaluateService.findByEvaluatePage(pagination);
        model.addAttribute("paginationOne",pagination);
        return "evaluate";
    }

    @PostMapping(value = "evaluate")
    @ResponseBody
    public Pagination doEvaluate(@RequestBody Pagination pagination,Model model){
        pagination = evaluateService.findByEvaluatePage(pagination);
        return pagination;
    }



    @GetMapping(value = "complaint{status}")
    public String toComplaint(@PathVariable("status") Integer status, Model model){
        ComplaintOv complaintOv = new ComplaintOv();
        switch(status){
            case 0:
                complaintOv.setStatus(null);
                break;
            case 1:
                complaintOv.setStatus("已解决");
                break;
            case 2:
                complaintOv.setStatus("处理中");
                break;
        }

        Pagination pagination = complaintService.findByComplaintPage(complaintOv);
        model.addAttribute("pagination",pagination);
        return "complaint";
    }

    @PostMapping(value = "complaint")
    @ResponseBody
    public Pagination doComplaint(@RequestBody ComplaintOv complaintOv, Model model){
        Pagination pagination = complaintService.findByComplaintPage(complaintOv);
        return pagination;
    }

    @PostMapping(value = "selectComplaintDate")
    @ResponseBody
    public Pagination selectComplaintDate(@RequestBody ComplaintOv complaintOv, Model model) throws ParseException {
        if(complaintOv.getStartDate()!=""&&complaintOv.getStopDate()!=""){
        Pagination pagination = complaintService.findByComplaintPageDate(complaintOv);
            return pagination;
        }else {
            Pagination pagination = complaintService.findByComplaintPage(complaintOv);
            return pagination;
        }
    }
    @PostMapping(value = "updateComplaintStatus")
    @ResponseBody
    public String updateComplaintStatus(@RequestBody ComplaintOv complaintOv, Model model){
        try{complaintService.updateComplaintStatus(complaintOv);
        return "SUCCESS";
        }catch (Exception e){
            return "FAIL";
        }
    }

    @PostMapping(value = "deleteComplaintStatus")
    @ResponseBody
    public String deleteComplaintStatus(@RequestBody ComplaintOv complaintOv, Model model){
        try{complaintService.deleteComplaintStatus(complaintOv);
            return "SUCCESS";
        }catch (Exception e){
            return "FAIL";
        }
    }

    @PostMapping(value = "addComplaint")
    @ResponseBody
    public String addComplaint(@RequestBody Complaint complaint, Model model){
        try{complaintService.addComplaint(complaint);
            return "SUCCESS";
        }catch (Exception e){
            return "FAIL";
        }
    }
}
